<?php
/**
 * Created by
 * User: 小涛
 * Date: 2018/5/29
 * Time: 10:03
 */

namespace app\admin\controller;

use app\admin\model\UserType as UserTypeModel;
use think\Request;

/**
 * Class UserType  管理权限
 * @package app\admin\controller
 */
class UserType extends Admin
{
    //列表
    public function index(){
        $list = UserTypeModel::listUserType();
        $this -> assign("list",$list);
        return $this -> fetch();
    }

    //添加
    public function add(){
        $request = Request::instance();
        if($request -> isPost()){
            $data = $request -> param();
            $list = new  UserTypeModel($data);
            $list -> create_time = time();
            $list -> update_time = time();
            if($list -> save()){
                return "添加成功";
            }
            return "添加失败";
        }
        $permission = config("permission");
        $this->assign("permission",$permission);
        return $this -> fetch();
    }

    //编辑
    public function edit(){
        $request = Request::instance();
        $id = 0;
        $data = $request -> param();
        if(array_key_exists("id",$data)){ $id = $data["id"]; }
        $list = UserTypeModel::get($id);
        if($request -> isPost()){
            if($list){
                $list -> name = $data["name"];
                $list -> jurisdiction = $data["jurisdiction"];
                $list -> create_time = time();
                $list -> update_time = time();
                if($list -> save()){
                    return "编辑成功";
                }
            }
            return "编辑失败";
        }
        $this -> assign("list",$list);
        $permission = config("permission");
        $this->assign("permission",$permission);
        return $this -> fetch();
    }

    public function delete(){
        $request = Request::instance();
        if($request -> isPost()){
            $url = $request->module()."/". $request->controller()."/". $request->action();
            if(!UserTypeModel::Jurisdiction($url)){
                return "没有操作权限";
            }
            $data = $request -> param();
            $id = 0;
            if(array_key_exists("id",$data)){ $id = $data["id"]; }
            if($id == 1){
                return "删除失败";
            }
            $list = UserTypeModel::get($id);
            if($list -> delete()){
                return "删除成功";
            }
            return "删除失败";
        }
    }
}